#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#define MAXSIZE 100

int main()
{
    int n, k;
    setbuf(stdout,NULL);
    scanf("%d%d",&n, &k);
    assert(n>=1 &&n<100 && k>0);

    int current=0, next=1;
    bool life[2][MAXSIZE][MAXSIZE];
    //input array
    for (int i=0; i<n; i++){
        for (int j=0;j<n;j++) {
            int temp;
            scanf("%d", &temp);
            life[current][i][j] = (temp == 1);
            life[next][i][j] = 0;
        }
    }

    //演化 k次   
    for (int iter=0; iter<k; iter++) {
        for (int i=0;i<n;i++) {
            for (int j=0;j<n;j++) {
                int neighbor = 0;
                for (int dx=-1;dx<=1;dx++) {
                    for (int dy=-1; dy<=1; dy++) {
                        if (!(dx==0 && dy==0)) {
                            int x = i+dx;
                            int y = j+dy;
                            if (x>=0 && x<n && y>=0 && y<n && life[current][x][y])
                                neighbor++;
                        } 
                    }
                }
                if (life[current][i][j])
                    life[next][i][j] = (neighbor==2 || neighbor==3);
                else
                    life[next][i][j] = (neighbor==3);
            }
        }
        current = (current == 0) ? 1 : 0;
        next = (next == 0) ? 1 : 0;
    }

    //列印
    for (int i=0; i<n; i++){
        for (int j=0;j<n;j++) {
            if (j == n-1)
                printf("%d\n", life[current][i][j]);
            else
                printf("%d ", life[current][i][j]);
        }
    }
}